/* 
 * This file is part or JMathLib 
 * 
 * Check it out at http://www.jmathlib.de
 *
 * Author:  
 * (c) 2005-2009   
 */
package joctave.toolbox.joctave.system;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import joctave.core.tokens.Token;
import joctave.core.tokens.OperandToken;
import joctave.core.functions.ExternalFunction;
import joctave.core.interpreter.GlobalValues;

/**An external function for writing to the main display*/
public class fprintf extends ExternalFunction
{
	/**write operand to main display
	@param operand[n] = items to display*/
	public OperandToken evaluate(Token[] operands, GlobalValues globals)
	{
		if (operands.length==1)
			globals.getInterpreter().displayText(operands[0].toString());
		else if (operands.length>1){
			String format=operands[0].toString();
			String []strings=new String[operands.length-1];
			for(int index = 1; index < operands.length; index++){
				strings[index-1]=operands[index].toString();
			}
			ByteArrayOutputStream buff = new ByteArrayOutputStream();
			PrintStream ps = new PrintStream(buff);
			ps.printf(format, strings);
		    globals.getInterpreter().displayText(buff.toString());
		}
		
		return null; 
	}
}

/*
@GROUP
system
@SYNTAX
FPRINTF(string1, string2, string3, ...)
@DOC
This displays a set of lines of text.
@NOTES
@EXAMPLES
FPRINTF("%s %s","Hello", "world")
Hello
world
@SEE
*/
